package creational

import "sync"

type Singleton struct{}

var (
	instance *Singleton

	mux  sync.Mutex
	once sync.Once
)

// GetSingletonInstanceFromMux 懒汉模式带锁
func GetSingletonInstanceFromMux() *Singleton {

	if instance != nil {
		return instance
	}

	mux.Lock()
	defer mux.Unlock()

	if instance != nil {
		return instance
	}
	instance = &Singleton{}

	return instance
}

// GetSingletonInstanceFromOnce 底层实际是调用atomic.LoadUint32
func GetSingletonInstanceFromOnce() *Singleton {
	once.Do(func() {
		instance = &Singleton{}
	})
	return instance
}
